<?php


namespace app\api\controller;

use app\data\model\ParkIndustries;
use app\data\model\ParkRegionals;
use app\data\model\Parks;
use think\admin\Controller;

class Park extends Controller
{
    /**
     * 园区首页数据
     */
    public function list()
    {
        $data = [
            'hot_parks'     => Parks::query()
                ->where('is_hot', '=', 1)
                ->where('status', '=', 1)
                ->where('deleted', '=', 0)
                ->field(['id', 'park_name', 'cover_img'])
                ->order('id', 'desc')->limit(6)->select(),
            'hot_regionals' => ParkRegionals::query()
                ->where('is_hot', '=', 1)
                ->where('status', '=', 1)
                ->where('deleted', '=', 0)
                ->field(['id', 'regional_name', 'cover_img', 'remark'])
                ->order('id', 'desc')->select(),
        ];
        return_msg(200, 'success', $data);
    }

    /**
     * 园区搜索
     * @throws \think\db\exception\DbException
     */
    public function search()
    {
        $page       = input('page', 1);
        $limit      = input('limit', 100);
        $regionalId = input('regional_id', null);
        $industryId = input('industry_id', null);
        $parkName   = input('park_name', null);
        $list       = Parks::query()->field(['id', 'park_name', 'cover_img', 'regional_id', 'industry_id']);

        if ($regionalId !== '' && $regionalId !== null) {
            $list = $list->where('regional_id', '=', $regionalId);
        }
        if ($industryId !== '' && $industryId !== null) {
            $list = $list->whereFindInSet('industry_id', $industryId);
        }
        if ($parkName !== '' && $parkName !== null) {
            $list = $list->whereLike('park_name', $parkName);
        }
        $list = $list->order('id', 'desc')->paginate(['list_rows' => $limit ?: 100, 'page' => $page ?: 1]);

        return_msg(200, 'success', [
            'list'  => $list->items(),
            'page'  => $list->currentPage(),
            'total' => $list->total(),
        ]);
    }

    /**
     * 园区搜索参数
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function searchParams()
    {
        $data = [
            'regional' => ParkRegionals::query()->field(['id', 'regional_name'])->where('status', '=', 1)->where('deleted', '=', 0)->select(),
            'industry' => ParkIndustries::query()->field(['id', 'industry_name'])->where('status', '=', 1)->where('deleted', '=', 0)->select(),
        ];
//        $regional = [];
//        $industry = [];
//        $data = [
//            'regional' => $regional,
//            'industry' => $industry,
//        ];
        return_msg(200, 'success', $data);
    }

    /**
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     */
    public function detail()
    {
        $id   = input('park_id');
        $data = Parks::query()->with(['regionals', 'industry'])->where('id', '=', $id)->find();
        if (!$data) {
            return_msg(404, '园区不存在');
        }
        /**
         * @var Parks $data
         */
        $data->setAttr('regional_name', $data->regionals->regional_name ?? '-');
        $data->setAttr('industry_name', $data->industry->industry_name ?? '-');
        $industryIds = $data->industry_id ? explode(',', $data->industry_id) : [];
        $data->setAttr('industry_name', $industryIds ? ParkIndustries::query()->whereIn('id', $data->industry_id)->column('industry_name') : []);
        unset($data->regionals, $data->industry, $data->created_at);
        return_msg(200, 'success', $data);
    }
}
